// FontLib.cpp: implementation of the CFontLib class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "..\ActiveWnd\Fontlib.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CFontLib g_fonts;

CFontLib::CFontLib() : /*m_head(0),*/ m_tail(0)
{
	ZeroMemory(m_cache, sizeof(m_cache));
}

CFontLib::~CFontLib()
{
	for (int i=0; i<10; i++) 
	{
		if (m_cache[i].hfont) 
			if(!::DeleteObject(m_cache[i].hfont))
			{
				int a = 0;
				ASSERT(0);
			}
	}
}

HFONT CFontLib::CreateFont(int size, char *family)
{
	//	search the cache first
	for (int i=0; i<10; i++) {
		if (m_cache[i].IsSame(size,family,false,FW_NORMAL)) return m_cache[i].hfont;
	}

	//	if not found, create a new font here
	HDC hdc = ::GetDC(NULL);
	HFONT hfont = ::CreateFont(-MulDiv(size, GetDeviceCaps(hdc, LOGPIXELSY), 72), 0,
				0, 0, FW_NORMAL, FALSE, FALSE, 0, DEFAULT_CHARSET, OUT_DEVICE_PRECIS,
				CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, family);
	ReleaseDC(NULL,hdc);

	if (hfont == NULL) return NULL;

	FontCache * pCell = &m_cache[m_tail];
	if (pCell->hfont) VERIFY(::DeleteObject(pCell->hfont));

	pCell->hfont = hfont;
	pCell->size = size;
	pCell->bUnderLine=false;
    pCell->dwWeight = FW_NORMAL;
	lstrcpyn(pCell->face, family, 255);

	m_tail++;
	if (m_tail>=10) m_tail =0;

//	if (m_tail== m_head) m_head++;

	return hfont;
}
HFONT CFontLib::CreateFont(int size, char * family,BOOL bUnderLine)
{
	for (int i=0; i<10; i++) {
		if (m_cache[i].IsSame(size, family,bUnderLine,FW_NORMAL)) return m_cache[i].hfont;
	}

	//	if not found, create a new font here
	HDC hdc = ::GetDC(NULL);
	HFONT hfont = ::CreateFont(-MulDiv(size, GetDeviceCaps(hdc, LOGPIXELSY), 72), 0,
				0, 0, FW_NORMAL, FALSE, bUnderLine, 0, DEFAULT_CHARSET, OUT_DEVICE_PRECIS,
				CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_SWISS, family);
	ReleaseDC(NULL,hdc);

	if (hfont == NULL) return NULL;

	FontCache * pCell = &m_cache[m_tail];
	if (pCell->hfont) VERIFY(::DeleteObject(pCell->hfont));

	pCell->hfont = hfont;
	pCell->size = size;
	pCell->bUnderLine=bUnderLine;
    pCell->dwWeight = FW_NORMAL;
	lstrcpyn(pCell->face, family, 255);

	m_tail++;
	if (m_tail>=10) m_tail =0;

//	if (m_tail== m_head) m_head++;

	return hfont;
}

HFONT CFontLib::CreateFont(int size, char * family, BOOL bUnderLine, DWORD dwWeight)
{
    for (int i=0; i<10; i++) {
        if (m_cache[i].IsSame(size, family,bUnderLine,dwWeight)) return m_cache[i].hfont;
    }

    //	if not found, create a new font here
    HDC hdc = ::GetDC(NULL);
    HFONT hfont = ::CreateFont(-MulDiv(size, GetDeviceCaps(hdc, LOGPIXELSY), 72), 0,
        0, 0, dwWeight, FALSE, bUnderLine, 0, DEFAULT_CHARSET, OUT_TT_PRECIS,
        CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY, DEFAULT_PITCH | FF_SWISS, family);
    ReleaseDC(NULL,hdc);

    if (hfont == NULL) return NULL;

    FontCache * pCell = &m_cache[m_tail];
    if (pCell->hfont) VERIFY(::DeleteObject(pCell->hfont));

    pCell->hfont = hfont;
    pCell->size = size;
    pCell->bUnderLine=bUnderLine;
    pCell->dwWeight = dwWeight;
    lstrcpyn(pCell->face, family, 255);

    m_tail++;
    if (m_tail>=10) m_tail =0;

    //	if (m_tail== m_head) m_head++;

    return hfont;
}
